
package processors;

import java.util.StringTokenizer;
import readers.QueryStringHandler;

/**
 * This will create a processor given the web query string
 * @author Chris
 */
public class ProcessorFactory {
    private ProcessorFactory() { }
    
    //Generates a correct processor
    public static processor createProcessor(String query) {
        QueryStringHandler qsh = new QueryStringHandler(query);
        String elm = qsh.getElement("category");
        query = rebuildQuery(query);
        if(elm.compareTo("login") == 0)
            return new processorLogin(query);
        
        //Due to time and compatibility this does not return
        //what would be expected, use listQuizzes2 for the previous
        //functionality
        if(elm.compareTo("listQuizzes") == 0)
            return new processorNextAvailable(query);
        
        if(elm.compareTo("listQuizzes2") == 0)
            return new processorAvailableQuizzes(query);
        
        if(elm.compareTo("listTakenQuizzes") == 0)
            return new processorTakenQuizzes(query);
       
        if(elm.compareTo("getScores") == 0)
            return new processorQuizScores(query);
               
        if(elm.compareTo("setScores") == 0)
            return new processorQuizSetScores(query);
        
        if(elm.compareTo("getQuiz") == 0)
            return new processorQuizWords(query);
        
        return new processorLogin(query);
    }
    
    //Adding in this due to the fact I changed the way the iPhone will
    //be passing in information.  This will rebuid the query to make
    //the query correct
    private static String rebuildQuery(String query) {
        QueryStringHandler qsh = new QueryStringHandler(query);
        String temp = qsh.getElement("GID");
        if(temp == null || temp.equals("")) {
            return query;
        }
        StringTokenizer st = new StringTokenizer(temp, "_");
        if(st.countTokens() != 2) {
            return query;
        }
        
        temp = "&TN=" + st.nextToken() + 
                "&GN=" + st.nextToken();
        
        //Just add it onto the end of the query since they are unique
        //elements they shouldn't conflict with any other methods
        return query + temp;
    }
}
